package com.cloud.bc.controller.ajax;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcMomen;
import com.cloud.bc.model.entity.BcMomenComments;
import com.cloud.bc.model.entity.BcMomenPraises;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.request.MomenRequest;
import com.cloud.bc.model.request.VoiceListRequest;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.model.response.VoiceListResponse;
import com.cloud.bc.service.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/momen")
public class Ajax_MomenController {
    private final BcMomenService bcMomenService;
    private final BcUsersService bcUsersService;
    private final BcMomenPraisesService bcMomenPraisesService;
    private final BcMomenCommentsService bcMomenCommentsService;
    private final BcSinglesService bcSinglesService;

    /**
     * 动态列表
     */
    @GetMapping("list")
    public AjaxResp list(HttpServletRequest request, HttpServletResponse response,
                         MomenRequest req) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        if (null != userInfo && Convert.toInt(userInfo.getId(), 0) > 0) {
            req.setUserId(userInfo.getId());
        } else {
            //未登录
            if (req.getOrderBy().equals("friend") || req.getOrderBy().equals("me")) {
                ajaxResp.setSuccess(false);
                ajaxResp.setMsg("请登录后操作");
                return ajaxResp;
            }
        }
        Page page = new Page();
        page.setCurrent(Convert.toInt(req.getPage(), 1));
        page.setSize(5);
        IPage<BcMomen> list = bcMomenService.getMomenSearchList(page, req);
        ajaxResp.setList(list == null ? null : list.getRecords());
        return ajaxResp;
    }

    /**
     * 创建动态
     */
    @PostMapping("create")
    public AjaxResp create(HttpServletRequest request, HttpServletResponse response,
                           BcMomen req) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        req.setUserId(userInfo.getId());
        req.setIsCheck(1);//动态直接审核通过了
        if (!req.getLocationSwitch().equals(1)) {
            //如果不是1，就把坐标和地址去掉
            req.setLatitude("");
            req.setLocation("");
            req.setLocation("");
        }
        bcMomenService.save(req);
        return ajaxResp;
    }

    //点赞
    @GetMapping("praise/{momentId}")
    public AjaxResp praise(HttpServletRequest request, HttpServletResponse response,
                           @PathVariable("momentId") Integer momentId) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        if (null == userInfo || Convert.toInt(userInfo.getId(), 0) <= 0) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("登录后点赞");
            return ajaxResp;
        }
        BcMomenPraises bcMomenPraises = new BcMomenPraises();
        bcMomenPraises.setMomentId(momentId);
        bcMomenPraises.setUserId(userInfo.getId());
        BcSingles bcSingles = bcSinglesService.selectOne(new BcSingles().setUserId(userInfo.getId()));
        bcMomenPraises.setPhoto(bcSingles.getPhoto());
        bcMomenPraisesService.save(bcMomenPraises);
        //记录次数
        bcMomenCommentsService.praise(momentId);
        return ajaxResp;
    }

    //取消点赞
    @GetMapping("unpraise/{momentId}")
    public AjaxResp unpraise(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable("momentId") Integer momentId) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        if (null == userInfo || Convert.toInt(userInfo.getId(), 0) <= 0) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("登录后取消点赞");
            return ajaxResp;
        }
        QueryWrapper<BcMomenPraises> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(BcMomenPraises::getMomentId, momentId);
        queryWrapper.lambda().eq(BcMomenPraises::getUserId, userInfo.getId());
        bcMomenPraisesService.remove(queryWrapper);
        //减次数
        bcMomenCommentsService.unpraise(momentId);
        return ajaxResp;
    }

    //删除单身圈
    @GetMapping("delete/{momentId}")
    public AjaxResp delete(HttpServletRequest request, HttpServletResponse response,
                           @PathVariable("momentId") Integer momentId) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        if (null == userInfo || Convert.toInt(userInfo.getId(), 0) <= 0) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("登录后删除");
            return ajaxResp;
        }
        QueryWrapper<BcMomen> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(BcMomen::getId, momentId);
        queryWrapper.lambda().eq(BcMomen::getUserId, userInfo.getId());
        bcMomenService.remove(queryWrapper);
        return ajaxResp;
    }

    //评论
    @PostMapping("comment")
    public AjaxResp comment(HttpServletRequest request, HttpServletResponse response,
                            BcMomenComments req) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        if (null == userInfo || Convert.toInt(userInfo.getId(), 0) <= 0) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("登录后评论");
            return ajaxResp;
        }
        req.setUserId(userInfo.getId());
        BcSingles bcSingles = bcSinglesService.selectOne(new BcSingles().setUserId(userInfo.getId()));
        req.setNickname(bcSingles.getNickname());
        req.setIsCheck(1);//审核通过
        bcMomenCommentsService.save(req);
        //记录次数
        bcMomenService.commentCount(req.getMomentId());
        return ajaxResp;
    }

}
